import {objDeepCopy} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	mounted() {
		this.getAllSeries(this);
		this.getSalesClassify();
	},
	methods: {
			
		//明确条码搜索
		onSearchSureBarCode(that) {
			const data = this.Model.filterOption;
			const params = {
				condition: {
					batchNumber: data.batchNumber || undefined,
					customCoding: data.customCoding || undefined,
				},
			};
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleOrder/getGoodsByCode',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result.result;
					this.Model.dialogTableData  = result;
					//分页
					this.Model.pager.params.pageSize = data.pageSize;
					this.Model.pager.params.pageIndex = data.pageIndex;
					this.Model.pager.pageTotal = data.pageTotal;
				}else{
					this.$message.error(data.msg);
				}
				
			});
		},
		//获取销售分类
		getSalesClassify() {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleGoodsAttribute/getGoodsAttributelist',
				data:{
					attributeCode:"M05"
				}
			}).then((res)=>{
				var data = res.data;				
				if(data.status == 200){
					var result = data.result;
					var arr = [];
					for(var i  = 0; i < result.length; i++){
						arr.push({
							label:result[i].attributeValueName,
							value:result[i].attributeValueCode
						});
					}
					this.Model.filterOption2.items[0].data = objDeepCopy(arr);
				}else{
					this.tips(this,'warning',data.msg)
				}
			    
			});
		},
		//获取系列
		getAllSeries(that) {
			//获取所有系列
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleGoodsAttribute/getGoodsAttributelist',
				data:{
					attributeCode:"M04"
				}
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					var result = data.result;
					var arr = [];
					for(var i  = 0; i < result.length; i++){
						arr.push({
							label:result[i].attributeValueName,
							value:result[i].attributeValueCode
						});
					}
					that.Model.filterOption2.items[1].data = objDeepCopy(arr);
				}else{
					this.tips(this,'warning',data.msg)
				}
			});
		},
		//输入系列修改款式名称
		changeStyleName(that,prams,option){
			prams = prams || {}
			this.$ajax({
		        method: 'post',
		        url:this.apiTest+'/sale/saleGoodsStyle/findStyleASeriesOf/'+prams,
		        data:{
		        	
		        }
		  	}).then((res)=>{
		        var data = res.data;
		        if(data.status == 200){
		        	var arr = [];
		        	var result = data.result;
					for (var i = 0; i < result.length; i++) {
						arr.push({
							label:result[i].modelName,
							value:result[i].id
						});
					}
					option[2].data = objDeepCopy(arr);
					that.Model.filterOption2.items = option;
		        }else{
					this.tips(this,'warning',data.msg)
		        }
		
		    });
		},
		//获取第二步数据
		getParameters(that,prams,option) {
			this.$ajax({
				method: 'get',
				url:this.apiTest+'/sale/saleGoodsStyle/condition/'+prams,
				data:{
					
				}
			}).then((res)=>{
				var data = res.data;	
				if(data.status == 200){				
					var result = data.result;
					console.log(result)
					//材质
					this.Model.filterOption3.items[3].data = result.attrMap.M15.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//手寸
					this.Model.filterOption3.items[4].data = result.handInchList.map(item => {
						return {
							label: item,
							value: item
						}
					});
					//净度
					this.Model.filterOption3.items[5].data = result.attrMap.M10.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//对称
					this.Model.filterOption3.items[6].data = result.attrMap.M13.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//抛光
					this.Model.filterOption3.items[7].data = result.attrMap.M12.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//链长
					this.Model.filterOption3.items[8].data = result.attrMap.M17.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//颜色
					this.Model.filterOption3.items[9].data = result.attrMap.M09.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					//切工
					this.Model.filterOption3.items[10].data = result.attrMap.M11.map(item => {
						return {
							label: item.attributeValueName,
							value: item.attributeValueCode
						}
					});
					var weightMin = result.range.fromTheRabbet*100;//钻重最小值
					var weightMax = result.range.theRabbet*100;//钻重最小值
					this.Model.weightMin=weightMin;
					this.Model.weightMax=weightMax;
					var weightArr =[];
					for (var i = weightMin; i < weightMax+1; i++) {				
						weightArr.push(i);
					}
					//钻重
					this.Model.sliderSelect = {
						weight:{
							value:'',
							option:weightArr.map(item => {
								return {
									label: item,
									value: item
								}
							}),
							sliderVal:[weightMin,weightMax],
							
						}
					}
				}else{
					this.tips(this,'warning',data.msg)
				}
			    
			});
			
		},
		//获取分组名称
		getTeamName() {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleGoodsGroup/list',
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						this.$set(result[i], 'isEdit', false)
						this.$set(result[i], 'active', false)
					}
					this.Model.TeamName = objDeepCopy(result);
					console.log(result)
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		//分组名称编辑
		Towrite(item,index) {
			// 非编辑状态下切换为编辑状态
			console.log(index)
			if (!item.isEdit) {
				item.isEdit = true;
				//聚焦输入框
				this.$nextTick(() => {
					this.$refs.teamInput[index].$el.querySelector('input').focus()
				});
			} else {
				// 编辑状态下保存，将tempLabel的值付给label，退出编辑状态

				item.isEdit = false
			}
		},
		//删除分组
		del (item,index) {
			this.Model.TeamName.splice(index, 1);
			let groupID = item.id;
			this.$ajax({
				method: 'DELETE',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroup/'+groupID,
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					this.$message({
						type: 'success',
						message: '已删除成功'
					});
					//this.Model.TeamName = objDeepCopy(result);
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		onSelectChange(type){
			const data = this.Model.sliderSelect[type];
			this.Model.sliderSelect[type].sliderVal = [data.value,data.value];
		},
		//加入分组
		addProduct(status) {
			/*this.Model.addProduct = status;	
			this.getTeamName();*/
			var data = this.Model.TableSelectData;
			if(data.length == 0){
				this.$message.error('请勾选数据');
			}else{
				this.Model.addProduct = status;	
				this.getTeamName();
			}	
		},
		//商品售价列表
		getProductList(that,params,pageIndex = 1,pageSize = 10) {
			params = params || {}
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleOrder/customListBySearch',
				//url:'/api/sale/saleOrder/customListBySearch',
				data:{
					condition:params,
					pageIndex: pageIndex,
					pageSize: pageSize,
				}
			}).then((res)=>{
				var data = res.data;	
				that.Model.dialogTableData = [];
				if(data.status == 200){
					var obj = {};
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					var result = data.result.result;
					console.log(data)
					for (var i = 0; i < result.length; i++) {				
						that.Model.dialogTableData.push(result[i]);
					}
				}else{
					this.tips(this,'warning',data.msg)
				}
			    
			});
		},
		handleCurrentChange(val) {
			let params = this.Model.Searchparams;
			this.getProductList(this,params,val);
		},
		handleSizeChange(val) {
			let params = this.Model.Searchparams;
			this.getProductList(this,params,undefined,val);
		},
		//新增商品里获取下拉框的事件
		showChild(val,changeOb,option) {
			switch (changeOb.key) {
				case 'series':
					var prams = val.series;
					this.changeStyleName(this,prams,option);
				break;
				case 'modelName':
					var prams = val.modelName;
					//获取第二步数据
					this.getParameters(this,prams,option);
				break;	
			}
			this.Model.searchObj = objDeepCopy(val);
		},
		showNotSureBarCodeP2(val){
			this.Model.searchFromData3 = val;
		},
		//无条码搜索
		toSearchNotSureBarCode(val) {
			let slider = this.Model.sliderSelect.weight.sliderVal;
			let params = objDeepCopy(this.Model.searchFromData3);
			params.salesType = this.Model.searchObj.origin;
			params.styleId = this.Model.searchObj.modelName;
			params.startInsert = slider[0]/100;
			params.endInsert = slider[1]/100;
			console.log(params);
			this.Model.Searchparams = params;
			this.getProductList(this,params)
			/*this.Model.tab1TableDetail1.flag = true;*/	
		},
		toClear(val) {
			this.Model.searchFromData2 = this.Model.defaultSearchFromData2;
			this.Model.searchFromData3 = this.Model.defaultSearchFromData3;
		},
		//确认加入分组
		addSure(val) {
			var mainTableData = this.Model.TableSelectData;
			var arr = [];
			for (var i = 0; i < mainTableData.length; i++) {	
				if(mainTableData[i].barCode){
					var obj = {
						saleGoodsGroupId:this.Model.chooseTeam,
						saleGoodsInfoId:mainTableData[i].id,
						isFinishProduct:0,
					}
				}else{
					var obj = {
						saleGoodsGroupId:this.Model.chooseTeam,
						saleGoodsInfoId:mainTableData[i].id,
						isFinishProduct:1
					}
				}
				if(mainTableData[i].saleGoodsMasterId){
					obj.saleGoodsMasterId = mainTableData[i].aSeriesOf
				}
				arr.push(obj);
			}
			this.$ajax({
				method: 'post',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroupDetail/addGoods',
				data:arr
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					var result = data.result;
					console.log(result);
					this.$message({
						type: 'success',
						message: '已加入成功'
					});
					this.Model.addProduct = false;
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		//修改组名
		SaveWrite (item) {
			item.isEdit = false;
			console.log(item.id)
			this.$ajax({
				method: 'put',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroup/update',
				data:{
					id:item.id,
					name:item.name
				}
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					this.$message({
						type: 'success',
						message: '已修改成功'
					});
					//this.Model.TeamName = objDeepCopy(result);
				}else{
					this.$message.error(data.msg);
				}
			});
			
		},
		//确认新增分组
		addSureteam() {
			let teamName = this.Model.Groupingname;
			if(teamName.length >= 10 ){
				this.$message.error('超出10个字符');
			}else{
				this.$ajax({
					method: 'post',
					//url:'/sale/saleGoodsGroupDetail/addGoods',
					url:this.apiTest+'/sale/saleGoodsGroup/add',
					data:{
						name:teamName
					}
				}).then((res)=>{
					var data = res.data;		
					if(data.status == 200){
						this.$message({
							type: 'success',
							message: '已新增成功'
						});
						this.getTeamName();
						this.Model.addNewteam = false;
					}else{
						this.$message.error(data.msg);
					}
				})
			}
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		addTeam(val) {
			console.log(val);
			this.Model.addNewteam =true;
		},
	}
}
export default Controller;
